package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 申述功能实现类
 */
@Service
public class ExplainServiceImpl extends ServiceImpl<ExplainMapper, Explain> implements ExplainService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的Explain表mapper对象
     */
    @Autowired
    private ExplainMapper _ExplainMpper;


    /**
     * 申述分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<ExplainDto> List(ExplainPagedInput input) {

      
        //声明一个支持申述查询的(拉姆达)表达式
        LambdaQueryWrapper<Explain> queryWrapper = Wrappers.<Explain>lambdaQuery()
                .eq(input.getId()!=null,Explain::getId,input.getId())
                .eq(input.getCreatorId()!=null,Explain::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入Title不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getTitle())) {
            queryWrapper=queryWrapper.eq(Explain::getTitle,input.getTitle());
       	 }
     	 //如果前端搜索传入Content不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getContent())) {
            queryWrapper=queryWrapper.eq(Explain::getContent,input.getContent());
       	 }
     	 //如果前端搜索传入File不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getFile())) {
            queryWrapper=queryWrapper.eq(Explain::getFile,input.getFile());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(Explain::getCreationTime);
        //构建一个分页查询的model
        Page<Explain> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取申述数据
        IPage<Explain> pageRecords= _ExplainMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _ExplainMpper.selectCount(queryWrapper);

        //把Explain实体转换成Explain传输模型
        List<ExplainDto> items= Extension.copyBeanList(pageRecords.getRecords(),ExplainDto.class);
        for (ExplainDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个申述查询
     */
    @SneakyThrows
    @Override
    public ExplainDto Get(ExplainPagedInput input) {
       if(input.getId()==null)
        {
            return new ExplainDto();
        }
        PagedResult<ExplainDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new ExplainDto();
    }

    /**
     *申述创建或者修改
     */
    @SneakyThrows
    @Override
    public ExplainDto CreateOrEdit(ExplainDto input) {

        //声明一个申述实体
        Explain Explain = new Explain();

        //把前端传入的input参数拷贝到申述实体
        BeanUtils.copyProperties(Explain,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(Explain);

        //定义一个返回给前端的申述传输模型
        ExplainDto ExplainDto=new ExplainDto();

        //同理把操作的申述实体拷贝给申述传输模型
        BeanUtils.copyProperties(ExplainDto,Explain);

        //把传输模型返回给前端
        return ExplainDto;
    }



}
